# -*- coding: utf-8 -*-

"""
@Author      : xxxxxxxxxxxxxxx
@Time        : 2024/6/10 11:29
"""
import os
import sys
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# 字体加载
from matplotlib import font_manager, rcParams

font_path = r"./font.ttf"
font_manager.fontManager.addfont(font_path)
prop = font_manager.FontProperties(fname=font_path)

# 字体设置
rcParams['font.family'] = 'sans-serif'  # 使用字体中的无衬线体
rcParams['font.sans-serif'] = prop.get_name()  # 根据名称设置字体
rcParams['font.size'] = 10  # 设置字体大小
rcParams['axes.unicode_minus'] = False  # 使坐标轴刻度标签正常显示正负号


def rir_plot(rir_file, start_date, end_date, out_loc):
    df = pd.read_csv(rir_file)
    df = df[(df['BYMD(BJT)'] >= start_date) & (df['BYMD(BJT)'] <= end_date)]
    cases = df['caseName'].unique()
    result = dict()
    for case in cases:
        df_case = df[df['caseName'] == case]
        result[case] = df_case['RIR'].mean()

    species = list(result.keys())
    species.pop(-1)
    rir = list(result.values())
    rir.pop(-1)
    label = ['$AVOCs$', '$BVOCs$', '$NOx$', '$CO$', '温度', '湿度', '烷烃', '烯烃', '炔烃', '芳香烃', '卤代烃', '含氧']

    lins = 5
    a1top = 1

    fig = plt.figure(figsize=(9, 4))
    ax1 = fig.add_subplot(111)
    plt.bar(range(len(rir)), width=0.5, height=rir,
            color=['purple', '#12e193', '#c5c9c7', 'pink', 'r', 'brown', '#95d0fc',
                   '#029386', 'orange', '#96f97b', '#c20078', 'yellow'], edgecolor='black')
    plt.xticks(range(len(rir)), label, fontsize=12)
    plt.margins()
    plt.hlines(y=0, ls='-', xmin=-100, xmax=len(rir) * 2, colors='grey')
    plt.xlim(-0.5, len(rir) - 0.5)
    ax1.set_ylim(-1, a1top)
    ax1.set_yticks(np.linspace(-1, a1top, lins), fontsize=14)
    # plt.legend()

    os.makedirs(out_loc, exist_ok=True)
    plt.ylabel('$RIR$', fontsize=14)
    plt.savefig(os.path.join(out_loc, 'rir_plot.png'), bbox_inches='tight', dpi=600)


if __name__ == '__main__':
    if len(sys.argv) < 5:
        print(f"[Error]: Run with python {sys.argv[0]} './5_rir.csv' '2023-05-16 07' '2023-05-16 19' './picture'")
    else:
        rir_plot(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])
